

import java.util.StringTokenizer;

public class Analyzer {
	private String StringToAnalyze;

	public Analyzer(String str){
		StringToAnalyze = str;
	}

	public void Analyze(){
		Game g= Network.getGame();
		StringTokenizer st = new StringTokenizer(StringToAnalyze, " ;,");
		String token = st.nextToken();
		if(token.compareTo("HEALTH")==0){//wall health
			String token2 = st.nextToken();
			int i=0;
			if (token2.compareTo("D2")==0){
				i=15;//from 15 to 30 are wall sections on the right wall
			}
			
			while(st.hasMoreTokens()){
				g.get_wallSections(i).setHealth(Integer.parseInt(st.nextToken()));
				i++;
			}
		}
		else if(token.compareTo("MV")==0){//move
			String token2 = st.nextToken();
			int robotNumber = Integer.parseInt(st.nextToken());
			int xCoord = Integer.parseInt(st.nextToken());
			int yCoord = Integer.parseInt(st.nextToken());
			if (token2.compareTo("A"+g.get_myNum())==0){//attacker 1
				//assume that A1 is ourRobot
				g.get_OurRobots(robotNumber).move(xCoord, yCoord);
                                System.out.println(g.get_OurRobots(0).get_posX() + " , "
                                + g.get_OurRobots(0).get_posY());
			}
			else if (token2.compareTo("D1")==0){//defender 1
				//
				g.get_Def1Robots(robotNumber).move(xCoord, yCoord);
			}
			else if (token2.compareTo("D2")==0){//defender 2
				//
				g.get_Def2Robots(robotNumber).move(xCoord, yCoord);
			}
                        else{// if (token2.compareTo("A2")==0){//attacker 2
				//assume that A2 is friendRobot
				g.get_PartnerRobot(robotNumber).move(xCoord, yCoord);
			}
		}
		else if(token.compareTo("AT")==0){//attack
			// i don't think our client needs to parse this message
		}
		else if(token.compareTo("SH")==0){
			//this is also not for us 
		}
		else if(token.compareTo("WL")==0){//set wall to have certain health
			String token2 = st.nextToken();
			int wall=0;
			int section = Integer.parseInt(st.nextToken());
			int newHealth = Integer.parseInt(st.nextToken());
			if(token2.compareTo("1")==0){//left wall
				wall = section;
			}
			else if(token2.compareTo("2")==0){//right wall
				wall = section +15;
			}
			Wall_Section w = g.get_wallSections(wall);
			w.setHealth(newHealth);
		}
		else if(token.compareTo("MSG")==0){
                        String who;
                        st.nextToken();
			String Message="";
                        //st.nextToken();
                        who = st.nextToken();
			while(st.hasMoreTokens()){
				Message += st.nextToken() + " ";
			}
                        god.letOthersKnow(who, Message);
		}
		else if(token.compareTo("JOIN")==0){
			//nothing to be done
		}
		else if(token.compareTo("STARTGAME")==0){
                    boolean haveFriend, twoDefenders;
                    if (st.nextToken().compareTo("1")==0){
                        haveFriend=false;
                    }
                    else{
                        haveFriend=true;
                    }
                    st.nextToken();
                    if (st.nextToken().compareTo("1")==0){
                        twoDefenders=false;
                    }
                    else{
                        twoDefenders=true;
                    }
                    st.nextToken();
                    while(st.hasMoreTokens()){
                        if(st.nextToken().compareTo("D")==0){
                            if(st.nextToken().compareTo("1")==0){
                                g.D1Name=st.nextToken();
                            }
                            else{
                                g.D2Name=st.nextToken();
                            }
                        }
                        else{
                            String number, name;
                            number = st.nextToken();
                            name = st.nextToken();
                            if(name.compareTo(g.myName) == 0)
                                g.set_myNum(Integer.parseInt(number)) ;
                            else{
                                g.friendName = name;
                            }
                        }
                    }

                    System.out.println("server -> STARTGAME");
			g.init(haveFriend , twoDefenders);
                        god.updateOurRobots();
                        System.out.println(g.get_OurRobots(0).get_posX() + " , "
                                + g.get_OurRobots(0).get_posY());
			//Startgame message needs to be changed.
		}
		else if(token.compareTo("ENDGAME")==0){
			//redundant message.
		}
		else if(token.compareTo("END_GAME")==0){
			String token2 = st.nextToken();
			if(token2.compareTo("Attacker")==0){
				//attackers win
			}
			else if(token2.compareTo("Defender")==0){
				//defenders win
			}
			else{
				//not good
			}
		}
		else if(token.compareTo("INTR")==0){
			//insert some variables to 
		}
		else if(token.compareTo("ENDTURN")==0){
			g.finishTurn();
		}
                else if(token.compareTo("BR")==0){
                    String token2 = st.nextToken();
                    if (token2.compareTo("A"+g.get_myNum())==0){
                        st.nextToken();
                        g.buildRobot(0, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                        god.updateOurRobots();
                    }
                    else if(token2.compareTo("D1")==0){
                        st.nextToken();
                        g.buildRobot(2, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                    }
                    else if(token2.compareTo("D2")==0){
                        st.nextToken();
                        g.buildRobot(3, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                    }
                    else{
                        st.nextToken();
                        g.buildRobot(1, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                    }
                }
                else if(token.compareTo("1")==0){
                    g.set_myNum(1);
                }
                else if(token.compareTo("2")==0){
                    g.set_myNum(2);
                }
                else{
                    //ignore;
                }
          god.updateGUI();
	}
}

/*
1.2.1	Wall health
String format:	HEALTH; D1; 100, 80, 70, 10, 30, 30, 10, 10, 01, 100, 100, 100, 100, 100, 100 
Meaning:	Wall at 1,0 gets a health of 100; Wall at 1,1 gets a health of 80 etc.

1.2.2	Move to location
String format:	 MV; A1,1; 2,5                 	
Meaning:	attacker 1 robot 1 move to grid 2, 5

1.2.3	Attack
String format:	AT; A2,1; 1,5; 10            	
Meaning:	Attack with robot 1 from 2nd attacker to wall at 1,5 damage of 10.

1.2.4	Shield
String format:	SH; 1,5
Meaning:                          	Shield at wall 1,5

1.2.5	Wall
String format:	WL; 1,5; 60                      	
Meaning:	The wall at 1,5 has a new health of 60

1.2.5	Messaging
String format:	MSG; PUB; chat message          
Meaning: 	Public chat message

String format:	MSG; PRI; ATT; CHAT MESSAGE
Meaning:	Private chat message for the attackers

1.2.6 	Create game
String format:	JOIN  
Meaning:     	Makes the server for clients to join

1.2.7	Start game
String format:	STARTGAME;2;2
Meaning:   	When the server receives this message, it stops waiting for clients and start the game

1.2.8	End game (Attackers won)
String format:	ENDGAME
Meaning:	Sent by the defense client. When the server receives this message, it stops waiting for clients to make moves and ends the game.

1.2.8.1	Declaring the winner 
String format:	END_GAME; Attacker
Meaning:	Declaring Attacker as winner. If timer is up, server sends END_GAME; Defender

1.2.9	Join a game
String format:	INTR; ATT; Rui	
Meaning:	Rui joined the game as an attacker. defender will be named DEF

1.2.10	Abandon/end a turn
String format: END
Meaning:	I am done with all my actions. Stop waiting and move onto the next player��s turn.

 */